﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.ComponentModel;
using System.Collections.ObjectModel;
//
// Mp3Cleaner, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
namespace Mp3Cleaner
{
    public class CData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        //
//_FileTrack_Changed||_FileName_Changed||_tagTrack_Changed||_tagTitle_Changed||_tagArtist_Changed||_tagAlbum_Changed||_tagYear_Changed||_tagGenre_Changed

        //
        //private bool _Marker;
        //public bool Marker {get { return _Marker; } set { _Marker = value; NotifyPropertyChanged(); }}
        //private bool _Marker;
        public bool Marker { get { return _FileTrack_Changed || _FileName_Changed || _tagTrack_Changed || _tagTitle_Changed || _tagArtist_Changed || _tagAlbum_Changed || _tagYear_Changed || _tagGenre_Changed; } }
        //
        private string _Disk_FullFileName;
        public string Disk_FullFileName { get { return null == _Disk_FullFileName ? "" : _Disk_FullFileName; } set {_Disk_FullFileName = null == value ? "" : value; NotifyPropertyChanged();}}
        //
        //
        //
        private string _FileTrack;
        public string FileTrack { get { return null == _FileTrack ? "" : _FileTrack; }
            set { string nv = null == value ? "" : value; _FileTrack = nv; if (FileNameExtChanged()) { _FileTrack_Changed = true; /*_FileName_Changed = true;*/ NotifyPropertyChanged(); } else { _FileTrack_Changed = false; _FileName_Changed = false; _FileExt_Changed = false; } }
        }
        //
        private bool _FileTrack_Changed;
        public bool FileTrack_Changed { get { return _FileTrack_Changed; } /*set { _FileTrack_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        //
        private string _FileName;
        public string FileName { get { return null == _FileName ? "" : _FileName; }
            set { string nv = null == value ? "" : value; _FileName = nv; if (FileNameExtChanged()) { _FileName_Changed = true; NotifyPropertyChanged(); } else { _FileTrack_Changed = false; _FileName_Changed = false; _FileExt_Changed = false; } }
        }
        //
        private bool _FileName_Changed;
        public bool FileName_Changed { get { return _FileName_Changed; } set { _FileName_Changed = value; NotifyPropertyChanged(); } }
        //
        //
        private string _FileExt;
        public string FileExt { get { return null == _FileExt ? "" : _FileExt; }
            set { string nv = null == value ? "" : value; _FileExt = nv; if (FileNameExtChanged()) { _FileExt_Changed = true; NotifyPropertyChanged(); } else { _FileTrack_Changed = false; _FileName_Changed = false; _FileExt_Changed = false; } }
        }
        //
        private bool _FileExt_Changed;
        public bool FileExt_Changed { get { return _FileExt_Changed; } set { _FileExt_Changed = value; NotifyPropertyChanged(); } }
        //
        //
        public bool FilePartChanged { get { return _FileTrack_Changed || _FileName_Changed || _tagTrack_Changed; } }
        //
        //
        //
        private string _tagTrack;
        public string tagTrack { get { return null == _tagTrack ? "" : _tagTrack; }
            set { string nv = null == value ? "" : value; if (nv != _tagTrack) { _tagTrack = nv; _tagTrack_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagTrack_Changed;
        public bool tagTrack_Changed { get { return _tagTrack_Changed; } /*set { _tagTrack_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        private string _tagTitle;
        public string tagTitle { get { return null == _tagTitle ? "" : _tagTitle; }
            set { string nv = null == value ? "" : value; if (nv != _tagTitle) { _tagTitle = nv; _tagTitle_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagTitle_Changed;
        public bool tagTitle_Changed { get { return _tagTitle_Changed; } /*set { _tagTitle_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        private string _tagArtist;
        public string tagArtist { get { return null == _tagArtist ? "" : _tagArtist; }
            set { string nv = null == value ? "" : value; if (nv != _tagArtist) { _tagArtist = nv; _tagArtist_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagArtist_Changed;
        public bool tagArtist_Changed { get { return _tagArtist_Changed; } /*set { _tagArtist_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        private string _tagAlbum;
        public string tagAlbum { get { return null == _tagAlbum ? "" : _tagAlbum; }
            set { string nv = null == value ? "" : value; if (nv != _tagAlbum) { _tagAlbum = nv; _tagAlbum_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagAlbum_Changed;
        public bool tagAlbum_Changed { get { return _tagAlbum_Changed; } /*set { _tagAlbum_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        private string _tagYear;
        public string tagYear { get { return null == _tagYear ? "" : _tagYear; }
            set { string nv = null == value ? "" : value; if (nv != _tagYear) { _tagYear = nv; _tagYear_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagYear_Changed;
        public bool tagYear_Changed { get { return _tagYear_Changed; } /*set { _tagYear_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        private string _tagGenre;
        public string tagGenre { get { return null == _tagGenre ? "" : _tagGenre; }
            set { string nv = null == value ? "" : value; if (nv != _tagGenre) { _tagGenre = nv; _tagGenre_Changed = true; NotifyPropertyChanged(); } }
        }
        //
        private bool _tagGenre_Changed;
        public bool tagGenre_Changed { get { return _tagGenre_Changed; } /*set { _tagGenre_Changed = value; NotifyPropertyChanged(); }*/ }
        //
        //
        public bool AnyTagChanged { get { return _tagTrack_Changed || _tagTitle_Changed || _tagArtist_Changed || _tagAlbum_Changed || _tagYear_Changed || _tagGenre_Changed; } }
        //public string tagGenre { get {return null==_?"":_;} set{_=null==value?"":value;} }
        //
        private void NotifyPropertyChanged(String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        //
        public CData()
        {
            setMembers("", "", "", "", "", "", "", "", "", "");
        }

        public CData(string Disk_FullFileName, string FileTrack, string FileName, string FileExt, string tagTrack, string tagTitle, string tagArtist, string tagAlbum, string tagYear, string tagGenre)
        {
            setMembers(Disk_FullFileName, FileTrack, FileName, FileExt, tagTrack, tagTitle, tagArtist, tagAlbum, tagYear, tagGenre);
        }
        //
        public CData(int n)
        {
            string s = n.ToString();
            setMembers("Disk_FullFileName" + s, "FileTrack" + s, "FileName" + s, "FileExt" + s, "tagTrack" + s, "tagTitle" + s, "tagArtist" + s, "tagAlbum" + s, "tagYear" + s, "tagGenre" + s);
        }
        //
        public void UnChanged()
        {
            //this.Marker = false;
            this._FileTrack_Changed = false;
            this._FileName_Changed = false;
            this._tagTrack_Changed = false;
            this._tagTitle_Changed = false;
            this._tagArtist_Changed = false;
            this._tagAlbum_Changed = false;
            this._tagYear_Changed = false;
            this._tagGenre_Changed = false;
        }
        //
        private void setMembers(string Disk_FullFileName, string FileTrack, string FileName, string FileExt, string tagTrack, string tagTitle, string tagArtist, string tagAlbum, string tagYear, string tagGenre)
        {
            this.Disk_FullFileName = Disk_FullFileName;
            this.FileTrack = FileTrack;
            this.FileName = FileName;
            this.FileExt = FileExt;
            this.tagTrack = tagTrack;
            this.tagTitle = tagTitle;
            this.tagArtist = tagArtist;
            this.tagAlbum = tagAlbum;
            this.tagYear = tagYear;
            this.tagGenre = tagGenre;
            //
            UnChanged();
            //
        }
        //
        private bool FileNameExtChanged()
        {
            string s = _FileTrack;
            if ("" != _FileTrack) s += " ";
            s += FileName + _FileExt;
            return (0 != String.Compare(_Disk_FullFileName, s, false));
        }
        //
        public class CompareStrings : IComparer<string>
        {
            public int Compare(string s1, string s2)
            {
                int i1, i2;
                if (Int32.TryParse(s1, out i1) && Int32.TryParse(s2, out i2))
                {
                    return i1 - i2;
                }
                return string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
            }
        }

        public static void Sort(ObservableCollection<CData> LD)
        {
            List<CData> ll = LD.ToList<CData>().OrderBy(x => x.FileTrack, new CompareStrings()).ThenBy(p => p.FileName).ToList<CData>();
            //
            //bool oldRaise = LD.RaiseListChangedEvents;
            //LD.RaiseListChangedEvents = false;
            try {
                LD.Clear();
                foreach (CData l in ll) LD.Add(l);
            } 
            finally {
                //LD.RaiseListChangedEvents = oldRaise;
            }
        }
        //
        //
    }//class
    public class QQ : ObservableCollection<CData>
    {
        private ObservableCollection<CData> LL = new ObservableCollection<CData>();
        //
        public QQ():base()
        {
            
        }
        public new void Add(CData cd)
        {
            LL.Add(cd);
        }
        public void Clear(CData cd)
        {
            LL.Clear();
        }
        public ObservableCollection<CData> GetAll()
        {
            return LL;
        }
    }
}//namespace
